﻿using Microsoft.AspNetCore.Builder;

namespace SBlogCore.Common.SwaggerExtend
{
    /// <summary>
    /// Swagger中间件
    /// </summary>
    public static class SwaggerMildd
    {
        public static void UseSwaggerMildd(this IApplicationBuilder app,Func<Stream> _Html)
        {
            // 将swagger首页，设置成我们自定义的页面，记得这个字符串的写法：{项目名.index.html}
            
            if (app == null) throw new ArgumentNullException(nameof(app));
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                if (_Html.Invoke() == null)
                {
                    var msg = "index.html的属性，必须设置为嵌入的资源";
                    //log.Error(msg);
                    throw new Exception(msg);
                }
                var version = "v1";
                //根据版本名称倒序 遍历展示
                var ApiName = AppSetting.ReadAppSettings("Startup:ApiName");
                c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"{ApiName} {version}");
                c.IndexStream = _Html;
                // 路径配置，设置为空，表示直接在根域名（localhost:7258）访问该文件,注意localhost:7258/swagger是访问不到的，去launchSettings.json把launchUrl去掉，如果你想换一个路径，直接写名字即可，比如直接写c.RoutePrefix = "doc";
                c.RoutePrefix = "";
            });
        }
    }
}
